package com.mouse.hadoop.explorer.controller;

import com.bianmaba.commons.bean.result.OperationResult;
import com.mouse.hadoop.explorer.pojo.FileStatusModel;
import com.mouse.hadoop.explorer.proxy.HdfsProxy;
import com.mouse.hadoop.explorer.service.HdfsService;
import org.apache.hadoop.fs.FileStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cwx183898 on 2017/7/13.
 */
@RestController
@RequestMapping("hdfs")
public class HdfsController {
    @Autowired
    private HdfsService hdfsService;

    @RequestMapping(path = "delete")
    public boolean delete(String path) {
        return hdfsService.delete(path);
    }

    @RequestMapping(path = "create_directory")
    public boolean createDirectory(String path, String name) {
        if (path.endsWith("/")) {
            path += name;
        } else {
            path += "/" + name;
        }
        return hdfsService.mkdir(path);
    }

    @RequestMapping(path = "get_file_status")
    public FileStatusModel getFileStatus(String path) {
        FileStatus fs = hdfsService.getFileStatus(path);
        if (fs == null) {
            return null;
        } else {
            return new FileStatusModel(fs);
        }
    }

    @RequestMapping(path = "upload")
    public OperationResult upload(String path, MultipartFile file) throws IOException {
        path = URLDecoder.decode(path, "UTF-8");
        String val = hdfsService.uploadFileToHdfs(path, file);
        return OperationResult.of(true).setResult("文件上传成功：" + val);
    }

    @RequestMapping(path = "get_children")
    public List<FileStatusModel> getChildren(String path) {
        List<FileStatusModel> list = new ArrayList<FileStatusModel>(0);
        FileStatus[] fses = hdfsService.listFiles(path, null);
        for (FileStatus fs : fses) {
            list.add(new FileStatusModel(fs));
        }
        return list;
    }

    @RequestMapping(value = "read")
    public String readFile(HttpServletResponse res, String path) {
        String content = hdfsService.openWithString(path);
        return content;
    }

    @RequestMapping(value = "download", method = RequestMethod.GET)
    public void downloadFile(HttpServletResponse res, String path) {
        int lastIndex = path.lastIndexOf("/");
        String fileName = path;
        if (lastIndex > -1) {
            fileName = path.substring(lastIndex + 1);
        }
        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        try {
            byte[] bytes = hdfsService.openWithBytes(path);
            OutputStream os = res.getOutputStream();
            os.write(bytes);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "save_file")
    public OperationResult saveFile(String path, String content) {
        boolean val = hdfsService.saveFile(path, content);
        return OperationResult.of(val);
    }

    @RequestMapping(value = "rename")
    public boolean rename(String path, String name) {
        return hdfsService.rename(path, name);
    }
}
